﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class FieldObject
    {
        private readonly string TableName;
        private readonly byte ModelId;
        private static readonly FieldDal MyDAL = new FieldDal();

        public FieldObject(byte _ModelId)
        {
            this.ModelId = _ModelId;
            this.TableName = ModelManage.GetTableName(this.ModelId);
        }

        public void Add(Field model)
        {
            string sql;
            if (model.FieldType == "int" || model.FieldType == "datetime")
            {
                //ALTER TABLE WinnerCMS_Article ADD test int NULL
                sql = string.Format("ALTER TABLE {0} ADD [{1}] {2} NULL", this.TableName,
                    model.FieldName,
                    model.FieldType);
            }
            else
            {
                sql = string.Format("ALTER TABLE {0} ADD [{1}] {2}({3}) NULL", this.TableName,
                    model.FieldName,
                    model.FieldType,
                    model.MaxFieldLength);
            }
            //if (model.DefaultValue.Length > 0)
            //{
            //    sql = sql + "\r\n" + string.Format("CONSTRAINT DF_{0}_{1} DEFAULT {2} ", TableName, model.FieldName, model.DefaultValue);
            //}
            MyDAL.AddField(sql);
        }

        #region 加载扩展字段界面

        /// <summary>
        /// 加载扩展字段界面
        /// </summary>
        /// <param name="T_DefinedFile"></param>
        /// <param name="ExtendField"></param>
        public void LoadUI(Table T_DefinedFile, out bool ExtendField)
        {
            IList<Field> list = MyDAL.FiedList(this.ModelId, false);
            if (list.Count == 0)
            {
                T_DefinedFile.Visible = false;
                ExtendField = false;
            }
            else
            {
                ExtendField = true;
                LoadUI(T_DefinedFile, list);
            }
        }

        /// <summary>
        /// 加载扩展字段界面
        /// </summary>
        /// <param name="T_DefinedFile"></param>
        /// <param name="CategoryId"></param>
        /// <param name="ExtendField"></param>
        public void LoadUI(Table T_DefinedFile, int CategoryId, out bool ExtendField)
        {
            IList<Field> list = MyDAL.GetShopModelField(this.ModelId, CategoryId);
            if (list.Count == 0)
            {
                T_DefinedFile.Visible = false;
                ExtendField = false;
            }
            else
            {
                ExtendField = true;
                LoadUI(T_DefinedFile, list);
            }
        }

        private void LoadUI(Table T_DefinedFile, IList<Field> list)
        {
            TableRow tr;
            TableCell tc;
            foreach (Field model in list)
            {
                tr = new TableRow();

                tc = new TableCell();
                tc.CssClass = "RowStyle";

                tc.Text = model.Title;


                tr.Controls.Add(tc);

                #region

                if (model.ControlType == "text")
                {
                    tc = new TableCell();
                    using (TextBox tb = new TextBox())
                    {
                        tb.ID = model.FieldName;
                        tb.Width = model.Width;
                        tb.TextMode = TextBoxMode.SingleLine;
                        tb.Text = model.DefaultValue;
                        tb.ToolTip = model.Tips;

                        tc.Controls.Add(tb);
                        tr.Controls.Add(tc);
                    }
                }
                if (model.ControlType == "password")
                {
                    tc = new TableCell();
                    using (TextBox tb = new TextBox())
                    {
                        tb.ID = model.FieldName;
                        tb.Width = model.Width;
                        tb.TextMode = TextBoxMode.Password;
                        tb.Text = model.DefaultValue;
                        tb.ToolTip = model.Tips;

                        tc.Controls.Add(tb);
                        tr.Controls.Add(tc);
                    }
                }
                if (model.ControlType == "select")
                {
                    tc = new TableCell();
                    using (DropDownList ddl = new DropDownList())
                    {
                        ddl.ID = model.FieldName;
                        ddl.Width = model.Width;
                        ddl.ToolTip = model.Tips;

                        foreach (string itme in model.DefaultValue.Split('\n'))
                        {
                            ddl.Items.Add(itme.Trim());
                        }
                        if (ddl.Items.Count > 0)
                            ddl.SelectedIndex = 0;

                        tc.Controls.Add(ddl);
                        tr.Controls.Add(tc);
                    }
                }
                if (model.ControlType == "radio")
                {
                    tc = new TableCell();
                    using (RadioButtonList rbl = new RadioButtonList())
                    {
                        rbl.ID = model.FieldName;
                        //rbl.Width = model.Width;
                        rbl.RepeatDirection = RepeatDirection.Horizontal;
                        rbl.RepeatLayout = RepeatLayout.Table;
                        rbl.ToolTip = model.Tips;

                        foreach (string itme in model.DefaultValue.Split('\n'))
                        {
                            rbl.Items.Add(itme.Trim());
                        }

                        if (rbl.Items.Count > 0)
                            rbl.SelectedIndex = 0;

                        tc.Controls.Add(rbl);
                        tr.Controls.Add(tc);
                    }
                }
                if (model.ControlType == "checkbox")
                {
                    tc = new TableCell();
                    using (CheckBox cb = new CheckBox())
                    {
                        cb.ID = model.FieldName;
                        cb.Width = model.Width;
                        cb.ToolTip = model.Tips;
                        try
                        {
                            cb.Checked = Convert.ToBoolean(model.DefaultValue);
                        }
                        catch
                        {
                        }

                        tc.Controls.Add(cb);
                        tr.Controls.Add(tc);
                    }
                }
                if (model.ControlType == "textarea")
                {
                    tc = new TableCell();
                    using (TextBox tb = new TextBox())
                    {
                        tb.ID = model.FieldName;
                        tb.Width = model.Width;
                        tb.TextMode = TextBoxMode.MultiLine;
                        tb.Height = 100;
                        tb.Text = model.DefaultValue;
                        tb.ToolTip = model.Tips;

                        tc.Controls.Add(tb);
                        tr.Controls.Add(tc);
                    }
                }

                #endregion

                T_DefinedFile.Controls.Add(tr);
            }
        }

        #endregion
    }
}